package com.mz.ozy.activity.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mz.ozy.R;
import com.mz.ozy.WpkApp;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ShogDetailBean;
import com.mz.ozy.mvp.presenter.IdyllicDetailPresenter;
import com.mz.ozy.mvp.view.IdyllicDetailContract;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.MyWebView;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import cn.xuzhijun.proportional.ProportionalFrameLayout;

public class IdyllicDetailActivity extends BaseActivity<IdyllicDetailContract.View, IdyllicDetailPresenter> implements IdyllicDetailContract.View {
    @BindView(R.id.vv_course_train_video)
    JzvdStd mVvCourseTrainVideo;
    @BindView(R.id.vv_idyllic_detail)
    ProportionalFrameLayout mVideoIdyllic;
    @BindView(R.id.iv_image)
    ImageView mIvImage;
    @BindView(R.id.tv_live_course_title)
    TextView mTvLiveCourseTitle;
    @BindView(R.id.wv_course_explain)
    WebView mWvCourseExplain;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idyllic_detail);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("详情");
        rightImage.setVisibility(View.GONE);
        rightImage.setImageResource(R.mipmap.icon_collect_star);
        mPresenter.getInfoDetailSuccess(this, getIntent().getStringExtra("id"));
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.resetAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void getInfoDetailSuccess(ShogDetailBean homeMerBean) {
        if (homeMerBean.getSctype() == 1) {
            mIvImage.setVisibility(View.GONE);
        } else {
            mVideoIdyllic.setVisibility(View.GONE);

        }

        if (homeMerBean.getSctype() == 1) {
            if (!TextUtils.isEmpty(homeMerBean.getVido())) {
                String proxyUrl = WpkApp.getProxy(this).getProxyUrl(homeMerBean.getVido());
                mVvCourseTrainVideo.setUp(proxyUrl, "");
                Glide.with(this).load(homeMerBean.getImg1()).into(mVvCourseTrainVideo.thumbImageView);
            }
        } else {
            GlideUtils.loadRoundImg(this, mIvImage, homeMerBean.getImg1(), R.mipmap.default_main_banner, 4);
        }
        if (!TextUtils.isEmpty(homeMerBean.getTitle())) {
            mTvLiveCourseTitle.setText(homeMerBean.getTitle());
        } else {
            mTvLiveCourseTitle.setText("");
        }
//        mWvCourseExplain.loadData(homeMerBean.getContent());
        mWvCourseExplain.loadDataWithBaseURL(null, setHtmlContent(Utils.getNewContent(homeMerBean.getContent())), "text/html", "utf-8", null);
    }

    public String setHtmlContent(String contents) {
        String head = ("<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;background-color:#ffffff;color:#000000;font-size:12px}</style>" +
                "</head>");
        return "<html>" + head + "<body style=\"word-wrap:break-word;\">" + contents + "</body>" + "</html>";
    }
}
